/*
 * Copyright (C) 2008 Matt Falkoski
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.falko.android.raven.settings;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.preference.Preference;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;

import com.falko.android.raven.R;

public class ColorChooserPreference extends Preference implements
		OnSeekBarChangeListener {

	private final String TAG = getClass().getName();
	private SeekBar redSeekBar_ = null;
	private SeekBar greenSeekBar_ = null;
	private SeekBar blueSeekBar_ = null;
	private SeekBar alphaSeekBar_ = null;
	private View colorPreview_ = null;
	private long userSelection_ = 0;

	public ColorChooserPreference(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public ColorChooserPreference(Context context, AttributeSet attrs,
			int defStyle) {
		super(context, attrs, defStyle);

	}

	@Override
	protected View onCreateView(ViewGroup parent) {

		LinearLayout layout = null;

		try {
			LayoutInflater mInflater = (LayoutInflater) getContext()
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

			layout = (LinearLayout) mInflater.inflate(
					R.layout.color_chooser_preference, parent, false);
		} catch (Exception e) {
			Log.e(TAG, "Error creating color chooser preference", e);
		}

		colorPreview_ = layout.findViewById(R.id.colorChooserPreview);
		redSeekBar_ = (SeekBar) layout.findViewById(R.id.redSeekBar);
		greenSeekBar_ = (SeekBar) layout.findViewById(R.id.greenSeekBar);
		blueSeekBar_ = (SeekBar) layout.findViewById(R.id.blueSeekBar);
		alphaSeekBar_ = (SeekBar) layout.findViewById(R.id.alphaSeekBar);

		final SeekBar bars[] = { blueSeekBar_, greenSeekBar_, redSeekBar_,
				alphaSeekBar_ };

		for (int i = 3; i >= 0; --i) {
			bars[i].setMax(255);
			bars[i].setOnSeekBarChangeListener(this);
			bars[i].setProgress((int) ((userSelection_ >>> 8 * i) & 0xFF));
		}
				
		((FrameLayout) layout.findViewById(R.id.colorChooserframe))
				.setBackgroundColor(Color.WHITE);

		onProgressChanged(null, 0, false);

		return layout;
	}

	public void onProgressChanged(SeekBar seekBar, int progress,
			boolean fromUser) {
		colorPreview_.setBackgroundColor(Color.argb(
				255, // alpha
				redSeekBar_.getProgress(), greenSeekBar_.getProgress(),
				blueSeekBar_.getProgress()));
	}

	public void onStartTrackingTouch(SeekBar seekBar) {
		// not used
	}

	public void onStopTrackingTouch(SeekBar seekBar) {

		long temp = (((alphaSeekBar_.getProgress() & 0xFF) << 24)
				| ((redSeekBar_.getProgress() & 0xFF) << 16)
				| ((greenSeekBar_.getProgress() & 0xFF) << 8) | ((blueSeekBar_
				.getProgress() & 0xFF)));

		if (!callChangeListener(temp)) {
			return;
		}

		userSelection_ = temp;
		persistLong(userSelection_);
		notifyChanged();
	}
	
	@Override
	protected Object onGetDefaultValue(TypedArray ta, int index) {

		return ta.getInt(index, 0);
	}

	@Override
	protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {

		if (restoreValue) {
			userSelection_ = getPersistedLong(userSelection_);
		} else {
			userSelection_ = Integer.parseInt(defaultValue.toString());
			persistLong(userSelection_);
		}
	}

}
